<?php
/**
 +------------------------------------------------------------------------------
 * 控制器实现类
 +------------------------------------------------------------------------------
 */
abstract class Controller extends IController
{
    protected $in;
    protected $json;
    protected $user = array();
    protected $page = array();
    protected $data = array();
    protected $JS   = array();

    /**
     +----------------------------------------------------------
     * 初期化（pre）
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    function preDispatch()
    {
    	// 页面基本信息
        $this->page = INI('page');
        $this->JS['page'] = INI('page');
        
        // 用户信息
        $this->user = FrameworkModel::getInstance()->User(ISession::get("uid"));
        
        // 常理信息
        $this->JS["CONST"] = ConstModel::getInstance()->ConstGroupValue();
        
        // 画面输入参数
        $this->json = new json();
    }
    
    /**
     +----------------------------------------------------------
     * 画面显示
     +----------------------------------------------------------
     * @access $view 画面名字
     * @access $data 数据
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    protected function dialogs($view, $data=array())
    {
    	// 画面变量设置
        $this->data['in'] = $this->in;
        $this->data['page'] = $this->page;
        $this->data['user'] = $this->user;
        $this->data['view_name']  = $view.EXT;
        $this->JS['user'] = $this->user;
        $this->data['JS'] = json_encode($this->JS);
        
        // 调用画面名称
        if(LOG_STATUS) Log::record('<br>调用view,访问的画面为：'.$view.EXT);
        
        // 程序性能数据
        if(LOG_STATUS) Log::record(sprintf('系统执行完毕，内存消耗：%01.4fKB，运行计时%01.4f秒', ((memory_get_usage()-FM_MEMONY)/1024), (microtime(true)-FM_RUNTIME)),'limegreen');
        
        // 业务层程序正常结束
        if(LOG_STATUS) Log::record('业务层程序正常结束！','limegreen');

        // 显示画面
        parent::dialogs($view, $data);
    }
    
    /**
     +----------------------------------------------------------
     * 画面显示
     +----------------------------------------------------------
     * @access $view 画面名字
     * @access $data 数据
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    protected function view($view,$expand='',$data=array())
    {
    	// 画面变量设置
        $this->data['in'] = $this->in;
        $this->data['page'] = $this->page;
        $this->data['user'] = $this->user;
        $this->data['view_name']  = $view.EXT;
        $this->JS['user'] = $this->user;
        $this->data['JS'] = json_encode($this->JS);
        
        // 调用画面名称
        if(LOG_STATUS) Log::record('<br>调用view,访问的画面为：'.$view.EXT);
        
        // 程序性能数据
        if(LOG_STATUS) Log::record(sprintf('系统执行完毕，内存消耗：%01.4fKB，运行计时%01.4f秒', ((memory_get_usage()-FM_MEMONY)/1024), (microtime(true)-FM_RUNTIME)),'limegreen');
        
        // 业务层程序正常结束
        if(LOG_STATUS) Log::record('业务层程序正常结束！','limegreen');
        
        // 显示画面
        parent::view($view,$expand,$data);
    }

    /**
     +----------------------------------------------------------
     * 取得画面的HTML
     +----------------------------------------------------------
     * @access $view 画面名字
     * @access $data 数据
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    protected function fetch($view,$expand='',$data=array())
    {
    	// 画面变量设置
    	$this->data['in'] = $this->in;
        $this->data['page'] = $this->page;
        $this->data['user'] = $this->user;
        $this->data['view_name']  = $view.EXT;
        $this->JS['user'] = $this->user;
        $this->data['JS'] = json_encode($this->JS);

        // 调用画面名称
        if(LOG_STATUS) Log::record('<br>调用view,访问的画面为：'.$view.EXT);
        
        // 程序性能数据
        if(LOG_STATUS) Log::record(sprintf('系统执行完毕，内存消耗：%01.4fKB，运行计时%01.4f秒', ((memory_get_usage()-FM_MEMONY)/1024), (microtime(true)-FM_RUNTIME)),'limegreen');
        
        // 业务层程序正常结束
        if(LOG_STATUS) Log::record('业务层程序正常结束！','limegreen');
        
        // 显示画面
		return parent::fetch($view,$expand,$data);
    }
    
    /**
     +----------------------------------------------------------
     * 取得画面的HTML
     +----------------------------------------------------------
     * @access $view 画面名字
     * @access $data 数据
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    protected function save($view, $data=array())
    {
    	// 缓存文件目录名字
    	$file_path = CACHE_PATH . G('folder') . DIR . G('file') . '.html';
    	
    	// 生成目录
		FileTool::dir(dirname($file_path));
		
    	// 画面变量设置
    	$content = $this->fetch($view,$data);
    	
    	// 写文件
    	FileTool::write($file_path, $content);
    	
    	// 显示
    	echo $content;    	
    }
    
	/**
     +----------------------------------------------------------
     * 取得画面的HTML
     +----------------------------------------------------------
     * @access $view 画面名字
     * @access $data 数据
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    protected function cache()
    {        
        // 显示画面
		return parent::cache();
    }
    
	/**
     +----------------------------------------------------------
     * 画面转移
     +----------------------------------------------------------
     * @access $url 新画面地址
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
    protected function redirect($url)
    {
    	header('Location: ' . $url);
    }
    
    /**
     +----------------------------------------------------------
     * 初期化（post）
     +----------------------------------------------------------
     * @access protected
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
	function postDispatch()
    {
    }
    
	/**
	 * 写日志
	 * Enter description here ...
	 * @param unknown_type $type 类型
	 * @param unknown_type $receive 信息接收员
	 * @param unknown_type $on 工单号
	 * @param unknown_type $jindu 进度
	 */
    protected function BLog($type,$receive,$business='',$jindu='')
    {
    	//初期化
    	$smsFlg=0;
    	$msgFlg=1;
    	$LogMsg="";
    	
    	//根据区分处理信息
    	switch ($type){
    		//无效工单
    		case BLogType::NG:
    			$LogMsg = BLogMsg::NG;
    			break;
    		//恢复工单
    		case BLogType::RENG:
    			$LogMsg = BLogMsg::RENG;
    			break;
    		//新工单
    		case BLogType::NEWS:
    			$LogMsg = BLogMsg::NEWS;
    			break;
    		//派工到机构
    		case BLogType::AGENCY:
    			$LogMsg = BLogMsg::AGENCY;
    			break;
    		//派工到个人
    		case BLogType::STAFF:
    			$LogMsg = BLogMsg::STAFF;
    			break;
    		//派工驳回
    		case BLogType::REDIS:
    			$LogMsg = BLogMsg::REDIS;
    			break;
    		//领取配件
    		case BLogType::RECEIVE:
    			$LogMsg = BLogMsg::RECEIVE;
    			break;
    		//施工
    		case BLogType::WORK:
    			$LogMsg = BLogMsg::WORK;
    			break;
    		//归还配件
    		case BLogType::RETURNS:
    			$LogMsg = BLogMsg::RETURNS;
    			break;
    		//施工进度
    		case BLogType::SCHEDULE:
    			$LogMsg = BLogMsg::SCHEDULE;
    			break;
    		//施工完成
    		case BLogType::WORKOK:
    			$LogMsg = BLogMsg::WORKOK;
    			break;
    		//完工审核
    		case BLogType::AUDIT:
    			$LogMsg = BLogMsg::AUDIT;
    			break;
    		//未通过完工审核
    		case BLogType::NOTAUDIT:
    			$LogMsg = BLogMsg::NOTAUDIT;
    			break;
    		//完工
    		case BLogType::OK:
    			$LogMsg = BLogMsg::OK;
    			break;
    	}
    	//整理信息
    	$param=array();
    	$param[2]=$this->user["cname"];
    	$param[3]=$business;
    	$param[4]=$jindu;
    	
    	//员工消息
    	BusinessLogTool::BusinessLog($type,$this->user["cid"],$LogMsg,$param,$business,$receive,0,$smsFlg,$msgFlg);
    }
    
	/**
	 * 顾客消息
	 * Enter description here ...
	 * @param unknown_type $type 类型
	 * @param unknown_type $receive 接收者
	 * @param unknown_type $complaints 咨询投诉ID
	 * @param unknown_type $param 参数
	 */
    protected function BSLog($type,$receive=0,$complaints='',$param=array())
    {
    	//初期化
    	$smsFlg=0;
    	$msgFlg=1;
    	$CustomerMsg="";
    	
    	//根据区分处理信息
    	switch ($type){
    		//无效工单
    		case BLogType::NG:
    			$CustomerMsg = BLogStaffMsg::NG;
    			break;
    		//恢复工单
    		case BLogType::RENG:
    			$CustomerMsg = BLogStaffMsg::RENG;
    			break;
    		//新工单
    		case BLogType::NEWS:
    			$CustomerMsg = BLogStaffMsg::NEWS;
    			break;
    		//派工到机构
    		case BLogType::AGENCY:
    			$CustomerMsg = BLogStaffMsg::AGENCY;
    			break;
    		//派工到个人
    		case BLogType::STAFF:
    			$CustomerMsg = BLogStaffMsg::STAFF;
    			break;
    		//派工驳回
    		case BLogType::REDIS:
    			$CustomerMsg = BLogStaffMsg::REDIS;
    			break;
    		//领取配件
    		case BLogType::RECEIVE:
    			$CustomerMsg = BLogStaffMsg::RECEIVE;
    			break;
    		//归还配件
    		case BLogType::RETURNS:
    			$CustomerMsg = BLogStaffMsg::RETURNS;
    			break;
    		//进度
    		case BLogType::SCHEDULE:
    			$CustomerMsg = BLogStaffMsg::SCHEDULE;
    			break;
    		//完工审核
    		case BLogType::AUDIT:
    			$CustomerMsg = BLogStaffMsg::AUDIT;
    			break;
    		//未通过完工审核
    		case BLogType::NOTAUDIT:
    			$CustomerMsg = BLogStaffMsg::NOTAUDIT;
    			break;
    		//完工
    		case BLogType::OK:
    			$CustomerMsg = BLogStaffMsg::OK;
    			break;
    	}
    	
    	//顾客信息
    	BusinessLogTool::BusinessSLog($type,$this->user["cid"],$CustomerMsg,$param,$complaints,$receive,1,$smsFlg,$msgFlg);
    }
}
